package com.company.cloud.mall.modules.data.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.mall.modules.cache.service.ShopDataCacheService;
import com.company.cloud.mall.modules.data.models.ShopSummaryData;
import com.company.cloud.mall.modules.data.models.ShopSummaryDataTotal;
import com.company.cloud.mall.modules.data.models.ShopSummaryModel;
import com.company.cloud.mall.modules.data.service.ShopStatisticService;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 商城数据统计
 */
@Authc
@RestController
@RequestMapping("/shop/data/statistic")
@Api(tags = "Shop_Data_商城数据统计")
public class ShopDataController {

    @Autowired
    private ShopStatisticService shopStatisticService;

    @Autowired
    private ShopDataCacheService shopDataCacheService;

    @GetMapping("/summary")
    @ApiOperation(value = "经营概况统计(今日/昨日)", response = ShopSummaryDataTotal.class)
    public BaseResult summary() {
//		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
//		ShopSummaryModel data= shopDataCacheService.shopSummaryStatisticByCompany(userInfo);
        
        String yesterday1 = StrUtil.format("{} 00:00:00", DateUtil.format(DateUtil.yesterday(), "yyyy-MM-dd"));
        String yesterday2 = StrUtil.format("{} 23:59:59", DateUtil.format(DateUtil.yesterday(), "yyyy-MM-dd"));

        String today1 = StrUtil.format("{} 00:00:00", DateUtil.format(new Date(), "yyyy-MM-dd"));
        String today2 = StrUtil.format("{} 23:59:59", DateUtil.format(new Date(), "yyyy-MM-dd"));

        ShopSummaryData yesterday = shopStatisticService.shopSummaryStatistic2Date(yesterday1, yesterday2);

        ShopSummaryData today = shopStatisticService.shopSummaryStatistic2Date(today1, today2);

        ShopSummaryDataTotal data = new ShopSummaryDataTotal();
        data.setYesterday(yesterday);
        data.setToday(today);
        return BaseResult.ok(data);
    }

    @GetMapping("/statis")
    @ApiOperation(value = "经营概况统计", response = ShopSummaryData.class)
    public BaseResult statis() {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        String dateStart = "2019-01-01 01:01:01";
        String dateEnd = DateUtil.now();
        ShopSummaryData data = shopDataCacheService.getShopSummaryData(dateStart, dateEnd);
        return BaseResult.ok(data);
    }


    @GetMapping("/statisByDate")
    @ApiOperation(value = "经营概况统计(根据日期)", response = ShopSummaryData.class)
    public BaseResult statisByDate(@RequestParam("type") String type) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        String dateStart;
        if (StrUtil.isEmpty(type)) {
            dateStart = "2019-01-01 01:01:01";
        } else if ("m".equals(type.toLowerCase())) {
            dateStart = DateUtil.format(DateUtil.beginOfMonth(new Date()), "yyyy-MM-dd HH:mm:ss");
        } else if ("w".equals(type.toLowerCase())) {
            dateStart = DateUtil.format(DateUtil.beginOfWeek(new Date()), "yyyy-MM-dd HH:mm:ss");
        } else {
            dateStart = "2019-01-01 01:01:01";
        }

        System.out.println("+++++ dateStart=" + dateStart);

        String dateEnd = DateUtil.now();
        ShopSummaryData data = shopDataCacheService.getShopSummaryData(dateStart, dateEnd);
        return BaseResult.ok(data);
    }


//	
//	@PostMapping("/orderFund")
//	@ApiOperation(value="成交额统计", response=BaseResult.class)
//	public BaseResult orderFund(@RequestBody OrderStatisticParam params) {
//		ValidatorUtils.validate(params);
//		LoginUserInfo userInfo= AuthContextUtils.getUser();
//		List<ShopOrderFundModel> data=shopStatisticService.orderFundStatisticByCompany(userInfo.getCustNo(), params, userInfo);
//		return BaseResult.ok(data);
//	}
//	
//	@PostMapping("/orderNum")
//	@ApiOperation(value="订单数统计", response=BaseResult.class)
//	public BaseResult orderNum(@RequestBody OrderStatisticParam params) {
//		return BaseResult.ok();
//	}
//	
//	@PostMapping("/orderPayNum")
//	@ApiOperation(value="付款订单统计", response=BaseResult.class)
//	public BaseResult orderPayNum(@RequestBody OrderStatisticParam params) {
//		return BaseResult.ok();
//	}
//	
//	@PostMapping("/payUserNum")
//	@ApiOperation(value="付款人数统计", response=BaseResult.class)
//	public BaseResult payUserNum(@RequestBody OrderStatisticParam params) {
//		return BaseResult.ok();
//	}
//	
//	@PostMapping("/returnOrder")
//	@ApiOperation(value="退款订单统计", response=BaseResult.class)
//	public BaseResult returnOrder(@RequestBody OrderStatisticParam params) {
//		return BaseResult.ok();
//	}

}
